package com.woniuxy.microshoppingcartserver.configure;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfigure {
    /**
     * 定义邮件队列
     * @return
     */
    @Bean
    public Queue emailQueue(){
        return new Queue("emailQueue",false);
    }
    /**
     * 直接交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        /*第一个参数：交换机名称;
         * 第二个参数：持久化支持;
         * 第三个参数：没有连接访问该交换机 ，不删除该交换机*/
        return new DirectExchange(
                "directExchange",false,false);
    }
    /**
     * 绑定队列与交换机之间的关系
     * @return
     */
    @Bean
    public Binding bindingDirectExchange(){
        return BindingBuilder.bind(emailQueue()).to(directExchange()).with("email");
    }
    /**
     * 主题交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange",false,false);
    }
    @Bean
    public Queue topicQueue(){
         return new Queue("topic.email",false);
    }
    /**
     * 绑定主题队列，以及主题交换机
     * @return
     */
    @Bean
    public Binding bindingTopicExchange(){
        return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("*.email");
    }
    /**
     * 扇形交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange",false,false);
    }
    @Bean
    public Queue queueA(){
        return new Queue("fanout.A",false);
    }
    @Bean
    public Queue queueB(){
        return new Queue("fanout.B",false);
    }
    /**
     * 将扇形交换机与某些队列进行绑定
     * @return
     */
    @Bean
    public Binding bindingFanoutExchangeA(){
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }
    /**
     * 将扇形交换机与某些队列进行绑定
     * @return
     */
    @Bean
    public Binding bindingFanoutExchangeb(){
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }
}